# -*- coding:utf-8 -*-
from selenium import webdriver

from appUtils import WaitSecond, browser_changeTopWindow, browser_doWait, waitInfiniti, getWebDriver

LoginName = "18133623141"
LoginPd = "Hrg123456"


def parseData(browser, list):
    """
    在当前页面取数据，下一页若还有数据则继续递归
    :param browser:
    :param list:
    :return:
    """
    table = browser.find_element_by_css_selector("table tbody");
    trs = table.find_elements_by_tag_name("tr")
    if (len(trs) < 1):
        return
    for tr in trs:
        tds = tr.find_elements_by_tag_name("td");
        list.append((tds[0].text, tds[1].text))
    #    下一页
    nextPage = browser.find_element_by_class_name("ucd-pager-next")
    if ("disabled" in nextPage.get_attribute("class")):
        # 不可用时说明没数据了
        return
    nextPage.click()
    browser_doWait(browser)
    parseData(browser, list)


def loadData(webProxyDriver):
    (browser, _, _) = webProxyDriver
    result = [];
    # 登录
    cards_ = "https://developer.huawei.com/consumer/cn/console#/serviceCards/"
    browser.get(cards_)
    browser.refresh()
    browser_doWait(browser)
    if (browser.current_url != cards_):
        browser.find_element_by_id("login_userName").send_keys(LoginName)
        browser.find_element_by_id("login_password").send_keys(LoginPd)
        browser.find_element_by_id("btnLogin").click()
    # 跳到下载量页面
    browser.get("https://developer.huawei.com/consumer/cn/console#/myReport/appMarket")
    browser_doWait(browser)
    # 查询上月下载量
    browser_changeTopWindow(browser)
    dateRangItem = browser.find_element_by_css_selector("div[class='form-item inline-block v-top ng-scope']")
    dateRangItem = dateRangItem.find_elements_by_tag_name("li")
    dateRangItem[0].click()
    browser.find_element_by_css_selector("a[class='btn btn-primary btn-small mr-2 ng-scope']").click()
    browser_doWait(browser)
    parseData(browser, result)
    # print(result)
    # 本月下载量
    dateRangItem[2].click()
    browser.find_element_by_css_selector("a[class='btn btn-primary btn-small mr-2 ng-scope']").click()
    browser_doWait(browser)
    parseData(browser, result)
    print("huawei",result)
    # browser.quit()
    # waitInfiniti()
    return result
